"""
Main GUI module.
Load the main frame and the menu bar.
"""

import tkinter as tk
from ui.MainFrame import MainFrame
from ui.MenuBar import MenuBar

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.menubar = MenuBar(self)
        self.mFrame = MainFrame(self)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.mFrame.grid(column=0,row=0,sticky=tk.N+tk.S+tk.E+tk.W)
        self.terminatedIncorrectly = False
        self.mFrame.update_input_frame()

        self.title('CRC UI')
        self.config(menu=self.menubar)
    def mainloop(self,n=0):
        """Launch the window."""
        try:
            tk.Tk.mainloop(self,n)
        except:
            # To avoid the crashed window problem.
            self.destroy()
            raise
        finally:
            try: # Closing the crc calc thread.
                if self.mFrame.resultFrame.calculationStarted:
                    if not self.mFrame.resultFrame.calculationOver:
                        self.mFrame.resultFrame.requestEarlyExit = True
                        self.mFrame.resultFrame.calcThread.join()
            # If it fails, no problem, the thread must not be launched
            except:
                pass
